package test.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import base.*;

public class Demo extends BaseObject{
	
	static final String TAG = "Demo";

	public static void demo1 () {
		PersonBean person = new PersonBean();  
        mLog.i(TAG, "Demo1: 包名: " + person.getClass().getPackage().getName() + "，"   
                + "完整类名: " + person.getClass().getName());  
	}
	
	  /** 
     * Demo2: 验证所有的类都是Class类的实例对象 
     * @throws ClassNotFoundException  
     */  
    public static void demo2() throws ClassNotFoundException  
    {  
        //定义两个类型都未知的Class , 设置初值为null, 看看如何给它们赋值成Person类  
        Class<?> class1 = null;  
        Class<?> class2 = null;  
          
        //写法1, 可能抛出 ClassNotFoundException [多用这个写法]  
        class1 = Class.forName("test.reflect.Person");  
        mLog.i(TAG, "Demo2:(写法1) 包名: " + class1.getPackage().getName() + "，"   
                + "完整类名: " + class1.getName());  
          
        //写法2  
        class2 = PersonBean.class;  
        mLog.i(TAG, "Demo2:(写法2) 包名: " + class2.getPackage().getName() + "，"   
                + "完整类名: " + class2.getName());  
    }  
    
    
    /** 
     * Demo3: 通过Java反射机制，用Class 创建类对象[这也就是反射存在的意义所在] 
     * @throws ClassNotFoundException  
     * @throws IllegalAccessException  
     * @throws InstantiationException  
     */  
    public static void demo3() throws ClassNotFoundException, InstantiationException, IllegalAccessException  
    {  
        Class<?> class1 = null;  
        class1 = Class.forName("test.reflect.Person");  
        //由于这里不能带参数，所以你要实例化的这个类Person，一定要有无参构造函数哈～  
        PersonBean person = (PersonBean) class1.newInstance();  
        person.setPersonAge(20);  
        person.setPersonName("LeeFeng");  
        mLog.i(TAG, "Demo3: " + person.getPersonName() + " : " + person.getPersonAge());  
    }  
    
    
    /** 
     * Demo4: 通过Java反射机制得到一个类的构造函数，并实现创建带参实例对象 
     * @throws ClassNotFoundException  
     * @throws InvocationTargetException  
     * @throws IllegalAccessException  
     * @throws InstantiationException  
     * @throws IllegalArgumentException  
     */  
    public static void demo4() throws ClassNotFoundException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException  
    {  
        Class<?> class1 = null;  
        PersonBean person1 = null;  
        PersonBean person2 = null;  
          
        class1 = Class.forName("test.reflect.Person");  
        //得到一系列构造函数集合  
        Constructor<?>[] constructors = class1.getConstructors();  
          
        person1 = (PersonBean) constructors[0].newInstance();  
        person1.setPersonAge(30);  
        person1.setPersonName("leeFeng");  
          
        person2 = (PersonBean) constructors[1].newInstance(20,"leeFeng");  
        
        mLog.i(TAG, "Demo4 -> constructors=" + constructors);  
        mLog.i(TAG, "Demo4: " + person1.getPersonName() + " : " + person1.getPersonAge()  
                + "  ,   " + person2.getPersonName() + " : " + person2.getPersonAge()  
        );  
    }  
    
    
    /** 
     * Demo5: 通过Java反射机制操作成员变量, set 和 get 
     *  
     * @throws IllegalAccessException  
     * @throws IllegalArgumentException  
     * @throws NoSuchFieldException  
     * @throws SecurityException  
     * @throws InstantiationException  
     * @throws ClassNotFoundException  
     */  
    public static void demo5() throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException, InstantiationException, ClassNotFoundException  
    {  
        Class<?> class1 = null;  
        class1 = Class.forName("test.reflect.Person");  
        Object obj = class1.newInstance();  
          
        Field personNameField = class1.getDeclaredField("name");  
        personNameField.setAccessible(true);  
        personNameField.set(obj, "胖虎先森");  
          
          
        mLog.i(TAG, "Demo5: 修改属性之后得到属性变量的值：" + personNameField.get(obj));  
          
    }  
    
    /** 
     * Demo6: 通过Java反射机制得到类的一些属性： 继承的接口，父类，函数信息，成员信息，类型等 
     * @throws ClassNotFoundException  
     */  
    public static void demo6() throws ClassNotFoundException  
    {  
        Class<?> class1 = null;  
        class1 = Class.forName("test.reflect.SuperMan");  
          
        //取得父类名称  
        Class<?>  superClass = class1.getSuperclass();  
        mLog.i(TAG, "Demo6:  SuperMan类的父类名: " + superClass.getName());  
          
        mLog.i(TAG, "===============================================");  
          
          
        Field[] fields = class1.getDeclaredFields();  
        for (int i = 0; i < fields.length; i++) {  
            mLog.i(TAG, "类中的成员: " + fields[i]);  
        }  
        mLog.i(TAG, "-------------------------");  
          
          
        //取得类方法  
        Method[] methods = class1.getDeclaredMethods();  
        for (int i = 0; i < methods.length; i++) {  
        	mLog.i(TAG, "i = " + i);
            mLog.i(TAG, "Demo6,取得SuperMan类的方法：");  
            mLog.i(TAG, "函数名：" + methods[i].getName());  
            mLog.i(TAG, "函数返回类型：" + methods[i].getReturnType());  
            mLog.i(TAG, "函数访问修饰符：" + Modifier.toString(methods[i].getModifiers()));  
            mLog.i(TAG, "函数代码写法： " + methods[i]);  
        }  
          
        mLog.i(TAG, "############################");  
        
        //取得类实现的接口,因为接口类也属于Class,所以得到接口中的方法也是一样的方法得到哈  
        Class<?> interfaces[] = class1.getInterfaces();  
        for (int i = 0; i < interfaces.length; i++) {  
        	mLog.i(TAG, "接口  i = " + i);
            mLog.i(TAG, "实现的接口类名: " + interfaces[i].getName() );  
        }  
          
    }  
    
    
    /** 
     * Demo7: 通过Java反射机制调用类方法 
     * @throws ClassNotFoundException  
     * @throws NoSuchMethodException  
     * @throws SecurityException  
     * @throws InvocationTargetException  
     * @throws IllegalAccessException  
     * @throws IllegalArgumentException  
     * @throws InstantiationException  
     */  
    public static void demo7() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException  
    {  
        Class<?> class1 = null;  
        class1 = Class.forName("test.reflect.SuperMan");  
          
        mLog.i(TAG, "Demo7: \n调用无参方法fly()：");  
        Method method = class1.getMethod("fly");  
        method.invoke(class1.newInstance());  
          
        mLog.i(TAG, "调用有参方法walk(int m)：");  
        method = class1.getMethod("walk",int.class);  
        method.invoke(class1.newInstance(),100);  
    }  
    
    

    /** 
     * Demo8: 通过Java反射机制得到类加载器信息 
     *  
     * 在java中有三种类类加载器。[这段资料网上截取] 
 
        1）Bootstrap ClassLoader 此加载器采用c++编写，一般开发中很少见。 
 
        2）Extension ClassLoader 用来进行扩展类的加载，一般对应的是jre\lib\ext目录中的类 
 
        3）AppClassLoader 加载classpath指定的类，是最常用的加载器。同时也是java中默认的加载器。 
     *  
     * @throws ClassNotFoundException  
     */  
    public static void demo8() throws ClassNotFoundException  
    {  
        Class<?> class1 = null;  
        class1 = Class.forName("test.reflect.SuperMan");  
        String nameString = class1.getClassLoader().getClass().getName();  
          
        mLog.i(TAG, "Demo8: 类加载器类名: " + nameString);  
    }  
      
    
    
    
    
}
